package com.dhcc.bpm.modules.scf.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dhcc.bpm.common.segmentId.LocalCacheIdGenerator;
import com.dhcc.bpm.common.utils.StringUtils;
import com.dhcc.bpm.modules.scf.dto.ScfSalesDetailsInfoDTO;
import com.dhcc.bpm.modules.scf.entity.ScfSalesDetailsInfo;
import com.dhcc.bpm.modules.scf.entity.SelectEntity.RecyclingInByPayment;
import com.dhcc.bpm.modules.scf.entity.SelectEntity.RecyclingWeighByRenewableOut;
import com.dhcc.bpm.modules.scf.entity.SelectEntity.RenewableOutByRenewableIn;
import com.dhcc.bpm.modules.scf.enumUtil.ScfCarbonEmissionsEnum;
import com.dhcc.bpm.modules.scf.mapper.*;
import com.dhcc.bpm.modules.scf.service.IScfSalesDetailsInfoService;
import com.dhcc.bpm.modules.scf.util.DateConversionExample;
import com.dhcc.bpm.modules.scf.util.ExcelUtil;
import com.dhcc.bpm.modules.scf.util.MD5Utils;
import com.dhcc.bpm.modules.scf.util.StringUtil;
import com.dhcc.bpm.modules.scf.vo.ScfContractInfoVO.ScfGetContractDetailsVO;
import com.dhcc.bpm.modules.scf.vo.ScfDriverInfoVO.ScfGetDriverDetailsVO;
import com.dhcc.bpm.modules.scf.vo.ScfInvoiceInfoVO.ScfGetInvoiceDetailsVO;
import com.dhcc.bpm.modules.scf.vo.ScfPaymentInfoVO.ScfGetPaymentDetailsVO;
import com.dhcc.bpm.modules.scf.vo.ScfRecyclingInInfoVO.ScfGetRecyclingInDetailsVO;
import com.dhcc.bpm.modules.scf.vo.ScfRecyclingWeighInfoVO.ScfGetRecyclingWeighDetailsVO;
import com.dhcc.bpm.modules.scf.vo.ScfRenewableInInfoVO.ScfGetRenewableInDetailsVO;
import com.dhcc.bpm.modules.scf.vo.ScfRenewableOutInfoVO.ScfGetRenewableOutDetailsVO;
import com.dhcc.bpm.modules.scf.vo.ScfSalesDetailsInfoVO.ScfGetSalesDetailVO;
import com.dhcc.bpm.modules.scf.vo.ScfSalesDetailsInfoVO.ScfGetSalesListVO;
import com.dhcc.bpm.modules.scf.vo.ScfSubjectBasicInfoVO.ScfGetSubjectDetailsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 销售明细表服务实现类
 *
 * @author liuguoting
 * @since 2023-10-12
 */
@Service
public class ScfSalesDetailsInfoServiceImpl extends ServiceImpl<ScfSalesDetailsInfoMapper, ScfSalesDetailsInfo> implements IScfSalesDetailsInfoService {

    /**
     * id生成器
     */
    @Autowired
    private LocalCacheIdGenerator localCacheIdGenerator;
    /**
     * 再生资源入库mapper接口
     */
    @Autowired
    private ScfRenewableInInfoMapper scfRenewableInInfoMapper;
    /**
     * 再生资源出库mapper接口
     */
    @Autowired
    private ScfRenewableOutInfoMapper scfRenewableOutInfoMapper;
    /**
     * 利废企业过磅mapper接口
     */
    @Autowired
    private ScfRecyclingWeighInfoMapper scfRecyclingWeighInfoMapper;
    /**
     * 利废企业入库mapper接口
     */
    @Autowired
    private ScfRecyclingInInfoMapper scfRecyclingInInfoMapper;
    /**
     * 合同信息mapper接口
     */
    @Autowired
    private ScfContractInfoMapper scfContractInfoMapper;
    /**
     * 主体信息mapper接口
     */
    @Autowired
    private ScfSubjectBasicInfoMapper scfSubjectBasicInfoMapper;
    /**
     * 司机信息mapper接口
     */
    @Autowired
    private ScfDriverInfoMapper scfDriverInfoMapper;
    /**
     * 支付信息mapper接口
     */
    @Autowired
    private ScfPaymentInfoMapper scfPaymentInfoMapper;
    /**
     * 发票信息mapper接口
     */
    @Autowired
    private ScfInvoiceInfoMapper scfInvoiceInfoMapper;

    /**
     * 列表查询
     *
     * @param page
     * @param scfSalesDetailsInfo
     * @return
     */
    @Override
    public IPage<ScfGetSalesListVO> selectScfSalesDetailsInfoPage(IPage<ScfGetSalesListVO> page, ScfSalesDetailsInfoDTO scfSalesDetailsInfo) {
        return page.setRecords(baseMapper.selectScfSalesDetailsInfoPage(page, scfSalesDetailsInfo));
    }

    /**
     * 详情查询
     *
     * @param scfSalesDetailsInfo
     * @return
     */
    @Override
    public ScfGetSalesDetailVO getDetail(ScfSalesDetailsInfoDTO scfSalesDetailsInfo) {
        return baseMapper.getDetail(scfSalesDetailsInfo.getRowId());
    }

    /**
     * 生成销售明细记录
     *
     * @param recyclingInNum
     */
    @Override
    public void generateSalesDetails(String recyclingInNum, String orgCode) {
        // new一个销售明细实体类，开始组合字段
        ScfSalesDetailsInfo scfSalesDetailsInfo = new ScfSalesDetailsInfo();
        // 设置orgCode
        scfSalesDetailsInfo.setOrgCode(orgCode);

        // 获取利废入库单rowId
        String RecyclingInRowId = scfRecyclingInInfoMapper.selectRowIdByRecyclingInNum(recyclingInNum);
        ScfGetRecyclingInDetailsVO scfRecyclingInInfoMapperDetails = scfRecyclingInInfoMapper.getDetails(RecyclingInRowId);
        if (scfRecyclingInInfoMapperDetails != null) {
            if (scfRecyclingInInfoMapperDetails!=null &&scfRecyclingInInfoMapperDetails.getContractNum() != null && StringUtils.isNotEmpty(scfRecyclingInInfoMapperDetails.getContractNum() )) {
                // 根据根据合同编号获取合同rowId
                String contractRowId = scfContractInfoMapper.selectRowIdByContractNum(scfRecyclingInInfoMapperDetails.getContractNum()  , scfRecyclingInInfoMapperDetails.getOrgCode());
                // 获取合同详细信息
                ScfGetContractDetailsVO detailsContract = scfContractInfoMapper.getDetails(contractRowId);
                String partyANum = detailsContract.getPartyANum();
                String roleByRowIdA = scfSubjectBasicInfoMapper.getRoleByRowId(partyANum);
                // 从供销合同中获取再生及供应商主体编号并查询主体信息
                ScfGetSubjectDetailsVO scfSubjectBasicInfoMapperDetails = null;
                if (roleByRowIdA.equals("3")) {
                    scfSubjectBasicInfoMapperDetails = scfSubjectBasicInfoMapper.getDetails(partyANum);
                    // 从主体信息中拿到主体名称并赋值
                    scfRecyclingInInfoMapperDetails.setRenewableName(scfSubjectBasicInfoMapperDetails.getSubName());
                    // 设置再生资源企业编号
                    scfSalesDetailsInfo.setRenewableSubNum(scfSubjectBasicInfoMapperDetails.getSubNum());
                } else if (roleByRowIdA.equals("2")) {
                    scfSubjectBasicInfoMapperDetails = scfSubjectBasicInfoMapper.getDetails(partyANum);
                    // 从主体信息中拿到主体名称并赋值
                    scfRecyclingInInfoMapperDetails.setRecyclingName(scfSubjectBasicInfoMapperDetails.getSubName());
                    // 设置购方企业编号
                    scfSalesDetailsInfo.setRecyclingSubNum(scfSubjectBasicInfoMapperDetails.getSubNum());
                }
                String partyBNum = detailsContract.getPartyBNum();
                String roleByRowIdB = scfSubjectBasicInfoMapper.getRoleByRowId(partyBNum);
                if (roleByRowIdB.equals("3")) {
                    scfSubjectBasicInfoMapperDetails = scfSubjectBasicInfoMapper.getDetails(partyBNum);
                    // 从主体信息中拿到主体名称并赋值
                    scfRecyclingInInfoMapperDetails.setRenewableName(scfSubjectBasicInfoMapperDetails.getSubName());
                    // 设置再生资源企业编号
                    scfSalesDetailsInfo.setRenewableSubNum(scfSubjectBasicInfoMapperDetails.getSubNum());
                } else if (roleByRowIdB.equals("2")) {
                    scfSubjectBasicInfoMapperDetails = scfSubjectBasicInfoMapper.getDetails(partyBNum);
                    // 从主体信息中拿到主体名称并赋值
                    scfRecyclingInInfoMapperDetails.setRecyclingName(scfSubjectBasicInfoMapperDetails.getSubName());
                    // 设置购方企业编号
                    scfSalesDetailsInfo.setRecyclingSubNum(scfSubjectBasicInfoMapperDetails.getSubNum());
                }


            }


            // 司机驾驶证号再去查询司机的详细信息


            if (scfRecyclingInInfoMapperDetails != null && StringUtils.isNotEmpty( scfRecyclingInInfoMapperDetails.getLicense() )) {
                String driverRowId = scfDriverInfoMapper.selectRowIdByLicense(scfRecyclingInInfoMapperDetails.getLicense());
                ScfGetDriverDetailsVO scfDriverInfoMapperDetails = scfDriverInfoMapper.getDetails(driverRowId);
                // 取出司机姓名及司机联系方式并设置给入库单详情中
                scfRecyclingInInfoMapperDetails.setDriverName(scfDriverInfoMapperDetails.getDriverName());
                scfRecyclingInInfoMapperDetails.setDriverMobile(scfDriverInfoMapperDetails.getMobile());
            }

            // 设置利废企业入库单号
            scfSalesDetailsInfo.setWarehouseEntNum(scfRecyclingInInfoMapperDetails.getRecyclingInNum());
            // 设置购方企业名称
            scfSalesDetailsInfo.setRecyclingName(scfRecyclingInInfoMapperDetails.getRecyclingName());
            // 设置再生资源主体名称
            scfSalesDetailsInfo.setRenewableName(scfRecyclingInInfoMapperDetails.getRenewableName());
            // 设置车牌号
            scfSalesDetailsInfo.setTruckNum(scfRecyclingInInfoMapperDetails.getTruckNum());
            // 设置司机（姓名）
            scfSalesDetailsInfo.setDriverName(scfRecyclingInInfoMapperDetails.getDriverName());
            // 设置包装重
            scfSalesDetailsInfo.setPackageWeight(String.valueOf(scfRecyclingInInfoMapperDetails.getTareWeight()));
            // 设置品质扣重
            scfSalesDetailsInfo.setQualityWeight(String.valueOf(scfRecyclingInInfoMapperDetails.getDeductWeight()));
            // 设置进场实重
            scfSalesDetailsInfo.setActualWeightIn(String.valueOf(scfRecyclingInInfoMapperDetails.getEntryWeight()));
            // 设置销售总金额
            scfSalesDetailsInfo.setTotalSalesAmount(scfRecyclingInInfoMapperDetails.getSettlementAmount());
            // 设置收货日期
            scfSalesDetailsInfo.setReceiveDate(scfRecyclingInInfoMapperDetails.getRecyclingInDate());

            String WeighNumRowId = scfRecyclingWeighInfoMapper.getRowIdByWeighNum(scfRecyclingInInfoMapperDetails.getWeighNum());


            if(StringUtils.isNotEmpty(WeighNumRowId  )){
                ScfGetRecyclingWeighDetailsVO recyclingWeighDetails = scfRecyclingWeighInfoMapper.getDetails(WeighNumRowId);
                // 设置重车重
                scfSalesDetailsInfo.setFullWeight(String.valueOf(recyclingWeighDetails.getFullWeight()));
                // 设置空车重
                scfSalesDetailsInfo.setEmptyWeight(String.valueOf(recyclingWeighDetails.getEmptyWeight()));
                // 利废过磅拿取再生出库单号
                // 先获取利废过磅单号-然后去利废过磅与再生出库关联关系表中查询对应的再生出库单号
                String weighNum = recyclingWeighDetails.getWeighNum();

                List<RecyclingWeighByRenewableOut> recyclingWeighByRenewableOuts = scfRecyclingWeighInfoMapper.selectRecyclingWeighByRenewableOut(weighNum);
                // 获取第一条数据
                RecyclingWeighByRenewableOut recyclingWeighByRenewableOut = recyclingWeighByRenewableOuts.get(0);
                // 在第一条数据中获取对应的再生出库单号
                String renewableOut = recyclingWeighByRenewableOut.getRenewableOutNum();

                // 通过再生出库单号查询对应的再生出库单的RowId
                String rowIdByRenewableOutNum = scfRenewableOutInfoMapper.selectRowIdByRenewableOutNum(renewableOut);
                // 获取再生出库单的详情
                ScfGetRenewableOutDetailsVO scfRenewableOutInfoMapperDetails = scfRenewableOutInfoMapper.getDetails(rowIdByRenewableOutNum);
                // 设置品名
                scfSalesDetailsInfo.setGoodsNum(scfRenewableOutInfoMapperDetails.getSpecName());
                // 设置规格
                scfSalesDetailsInfo.setStandardNum(scfRenewableOutInfoMapperDetails.getSpecName());
                // 设置销售单价
                scfSalesDetailsInfo.setUnitPrice(scfRenewableOutInfoMapperDetails.getUnitPrice());
                // 获取再生出库单号
                String renewableOutNum = scfRenewableOutInfoMapperDetails.getRenewableOutNum();
                // 通过再生出库单号查询再生出库与再生入库关联关系表
                List<ScfGetRenewableInDetailsVO> renewableOutByRenewableIns = scfRenewableOutInfoMapper.selectRenewableOutByRenewableInt(renewableOutNum);
                if(!renewableOutByRenewableIns.isEmpty()){
                    // 拿取第一条记录的再生入库单
                    ScfGetRenewableInDetailsVO renewableOutByRenewableIn = renewableOutByRenewableIns.get(0);
                    // 获取该条记录的再生入库单号
                    String renewableInNum = renewableOutByRenewableIn.getRenewableInNum();
                    // 通过再生入库单号码获取对应的再生入库单的rowId
                    String renewableInRowId = scfRenewableInInfoMapper.selectRowIdByRenewableInNum(renewableInNum);
                    // 根据再生入库单的rowId获取再生入库单的详情
                    ScfGetRenewableInDetailsVO scfRenewableInInfoMapperDetails = scfRenewableInInfoMapper.getDetails(renewableInRowId);
                    // 根据根据合同编号获取合同rowId
                    String contractRowIdren = scfContractInfoMapper.selectRowIdByContractNum(scfRenewableInInfoMapperDetails.getContractNum() , scfRecyclingInInfoMapperDetails.getOrgCode());
                    // 获取合同详细信息
                    ScfGetContractDetailsVO detailsContractren = scfContractInfoMapper.getDetails(contractRowIdren);
                    String partyANum1 = detailsContractren.getPartyANum();
                    String roleByRowIdA1 = scfSubjectBasicInfoMapper.getRoleByRowId(partyANum1);
                    // 从合同中获取再生及供应商主体编号并查询主体信息
                    ScfGetSubjectDetailsVO scfSubjectBasicInfoMapperDetails2 = null;

                    if (roleByRowIdA1.equals("1")) {
                        scfSubjectBasicInfoMapperDetails2 = scfSubjectBasicInfoMapper.getDetails(partyANum1);
                        // 从主体信息中拿到主体名称并赋值
                        scfRenewableInInfoMapperDetails.setSupplierName(scfSubjectBasicInfoMapperDetails2.getSubName());
                        // 设置供应商主体名称
                        scfSalesDetailsInfo.setOwnerName(scfRenewableInInfoMapperDetails.getSupplierName());
                        // 设置供应商编号
                        scfSalesDetailsInfo.setOwnerSubNum(scfSubjectBasicInfoMapperDetails2.getSubNum());
                    } else if (roleByRowIdA1.equals("3")) {
                        scfSubjectBasicInfoMapperDetails2 = scfSubjectBasicInfoMapper.getDetails(partyANum1);
                        // 从主体信息中拿到主体名称并赋值
                        scfRenewableInInfoMapperDetails.setRenewableName(scfSubjectBasicInfoMapperDetails2.getSubName());
                    }
                    String partyBNum1 = detailsContractren.getPartyBNum();
                    String roleByRowIdB1 = scfSubjectBasicInfoMapper.getRoleByRowId(partyBNum1);
                    if (roleByRowIdB1.equals("1")) {
                        scfSubjectBasicInfoMapperDetails2 = scfSubjectBasicInfoMapper.getDetails(partyBNum1);
                        // 从主体信息中拿到主体名称并赋值
                        scfRenewableInInfoMapperDetails.setSupplierName(scfSubjectBasicInfoMapperDetails2.getSubName());
                        // 设置供应商主体名称
                        scfSalesDetailsInfo.setOwnerName(scfRenewableInInfoMapperDetails.getSupplierName());
                        // 设置供应商编号
                        scfSalesDetailsInfo.setOwnerSubNum(scfSubjectBasicInfoMapperDetails2.getSubNum());
                    } else if (roleByRowIdB1.equals("3")) {
                        scfSubjectBasicInfoMapperDetails2 = scfSubjectBasicInfoMapper.getDetails(partyBNum1);
                        // 从主体信息中拿到主体名称并赋值
                        scfRenewableInInfoMapperDetails.setRenewableName(scfSubjectBasicInfoMapperDetails2.getSubName());
                    }
                    // 司机驾驶证号再去查询司机的详细信息
                    String driverRowIdren = scfDriverInfoMapper.selectRowIdByLicense(scfRenewableInInfoMapperDetails.getLicense());
                    ScfGetDriverDetailsVO scfDriverInfoMapperDetailsren = scfDriverInfoMapper.getDetails(driverRowIdren);
                    // 取出司机姓名及司机联系方式并设置给入库单详情中
                    scfRenewableInInfoMapperDetails.setDriverName(scfDriverInfoMapperDetailsren.getDriverName());
                    scfRenewableInInfoMapperDetails.setDriverMobile(scfDriverInfoMapperDetailsren.getMobile());
                    // 设置采购单价
                    scfSalesDetailsInfo.setPurchasePrice(scfRenewableInInfoMapperDetails.getUnitPrice());
                    // 设置付款重
                    scfSalesDetailsInfo.setPaymentWeight(String.valueOf(recyclingWeighDetails.getGoodsWeight()));
                    // 设置付给供货商货款
                    scfSalesDetailsInfo.setPaymentToOwner(scfRenewableInInfoMapperDetails.getSettlementAmount());
                }else{
                    System.out.println("再生出库单===为空 " +renewableOutNum);
                }



            }






            // 初始生成的销售明细，对账状态为0。暂未对账，其他状态为：1.对账失败 2.对账成功
            scfSalesDetailsInfo.setIsOk("0");
            // 目前 利废入库单已删除支付回单单号，用利废入库单号查询利废入库与支付关系表，获取支付回单数组
            List<RecyclingInByPayment> recyclingInByPayments = scfRecyclingInInfoMapper.selectRecyclingByPayment(recyclingInNum);
            String invoiceDate = null;
            if (recyclingInByPayments != null && !recyclingInByPayments.isEmpty()) {
                // 取集合中的第一条
                RecyclingInByPayment recyclingInByPayment = recyclingInByPayments.get(0);
                // 拿第一条数据中的支付回单号查询对应支付信息的RowId
                String selectRowIdBypaymentNum = scfPaymentInfoMapper.selectRowIdBypaymentNum(recyclingInByPayment.getPaymentNum());
                ScfGetPaymentDetailsVO scfPaymentInfoMapperDetail = scfPaymentInfoMapper.getDetail(selectRowIdBypaymentNum);
                String selectRowIdByPaymentNum = scfInvoiceInfoMapper.selectRowIdByPaymentNum(scfPaymentInfoMapperDetail.getPaymentNum());
                ScfGetInvoiceDetailsVO scfInvoiceInfoMapperDeatil = scfInvoiceInfoMapper.getDeatil(selectRowIdByPaymentNum);
                // 获取开票日期
                invoiceDate = scfInvoiceInfoMapperDeatil.getInvoiceDate();
            }
            scfSalesDetailsInfo.setInvoiceDate(invoiceDate);
            if(StringUtils.isNotEmpty(scfSalesDetailsInfo.getPaymentWeight()  )){
                Double goodsWeight = Double.valueOf(scfSalesDetailsInfo.getPaymentWeight());
                String goodsNum = scfSalesDetailsInfo.getGoodsNum();
                Double countCarbonEmissions = null;
                if (countCarbonEmissions == null) {
                    switch (goodsNum) {
                        case "废铝":
                            // 处理废铝的逻辑
                            countCarbonEmissions = ScfCarbonEmissionsEnum.ALUMINUM_RECYCLING.getEmissionFactor(goodsWeight);
                            break;
                        // 可以继续添加其他case语句处理不同的情况
                        case "废纸":
                            // 处理废铝的逻辑
                            countCarbonEmissions = ScfCarbonEmissionsEnum.PAPER_RECYCLING.getEmissionFactor(goodsWeight);
                            break;
                        // 可以继续添加其他case语句处理不同的情况
                        case "废钢":
                            // 处理废铝的逻辑
                            countCarbonEmissions = ScfCarbonEmissionsEnum.STEEL_RECYCLING.getEmissionFactor(goodsWeight);
                            break;
                        // 可以继续添加其他case语句处理不同的情况
                        case "废塑料":
                            // 处理废铝的逻辑
                            countCarbonEmissions = ScfCarbonEmissionsEnum.PLASTIC_RECYCLING.getEmissionFactor(goodsWeight);
                            break;
                        // 可以继续添加其他case语句处理不同的情况
                        case "废铜":
                            // 处理废铝的逻辑
                            countCarbonEmissions = ScfCarbonEmissionsEnum.COPPER_RECYCLING.getEmissionFactor(goodsWeight);
                            break;
                        case "废电器电子":
                            // 处理废铝的逻辑
                            countCarbonEmissions = ScfCarbonEmissionsEnum.ELECTRICAL_EQUIPMENT_RECYCLING.getEmissionFactor(goodsWeight);
                            break;
                        // 可以继续添加其他case语句处理不同的情况
                    }
                    // 设置碳排放量
                    scfSalesDetailsInfo.setCarbonEmissions(String.valueOf(countCarbonEmissions));
                }
            }
        }

        // 设置RowId
        // 生成时间

        String rowId = "XXMX" +    MD5Utils.md5( recyclingInNum  );
        scfSalesDetailsInfo.setRowId(rowId);

        // 生成销售明细数据
        saveOrUpdate(scfSalesDetailsInfo);

    }

    /**
     * 生成销售明细记录
     * 重载方法-适用于利废入库批量导入时 无orgCode参数
     *
     * @param recyclingInNum
     */
    @Override
    public void generateSalesDetails(String recyclingInNum) {
        // new一个销售明细实体类，开始组合字段
        ScfSalesDetailsInfo scfSalesDetailsInfo = new ScfSalesDetailsInfo();

        // 获取利废入库单rowId
        String RecyclingInRowId = scfRecyclingInInfoMapper.selectRowIdByRecyclingInNum(recyclingInNum);
        ScfGetRecyclingInDetailsVO scfRecyclingInInfoMapperDetails = scfRecyclingInInfoMapper.getDetails(RecyclingInRowId);
        // 根据根据合同编号获取合同rowId
        String contractRowId = scfContractInfoMapper.selectRowIdByContractNum(scfRecyclingInInfoMapperDetails.getContractNum()  , scfRecyclingInInfoMapperDetails.getOrgCode());
        // 获取合同详细信息
        ScfGetContractDetailsVO detailsContract = scfContractInfoMapper.getDetails(contractRowId);
        String partyANum = detailsContract.getPartyANum();
        String roleByRowIdA = scfSubjectBasicInfoMapper.getRoleByRowId(partyANum);
        // 从供销合同中获取再生及供应商主体编号并查询主体信息
        ScfGetSubjectDetailsVO scfSubjectBasicInfoMapperDetails = null;
        if (roleByRowIdA.equals("3")) {
            scfSubjectBasicInfoMapperDetails = scfSubjectBasicInfoMapper.getDetails(partyANum);
            // 从主体信息中拿到主体名称并赋值
            scfRecyclingInInfoMapperDetails.setRenewableName(scfSubjectBasicInfoMapperDetails.getSubName());
            // 设置再生资源企业编号
            scfSalesDetailsInfo.setRenewableSubNum(scfSubjectBasicInfoMapperDetails.getSubNum());
        } else if (roleByRowIdA.equals("2")) {
            scfSubjectBasicInfoMapperDetails = scfSubjectBasicInfoMapper.getDetails(partyANum);
            // 从主体信息中拿到主体名称并赋值
            scfRecyclingInInfoMapperDetails.setRecyclingName(scfSubjectBasicInfoMapperDetails.getSubName());
            // 设置购方企业编号
            scfSalesDetailsInfo.setRecyclingSubNum(scfSubjectBasicInfoMapperDetails.getSubNum());
        }
        String partyBNum = detailsContract.getPartyBNum();
        String roleByRowIdB = scfSubjectBasicInfoMapper.getRoleByRowId(partyBNum);
        if (roleByRowIdB.equals("3")) {
            scfSubjectBasicInfoMapperDetails = scfSubjectBasicInfoMapper.getDetails(partyBNum);
            // 从主体信息中拿到主体名称并赋值
            scfRecyclingInInfoMapperDetails.setRenewableName(scfSubjectBasicInfoMapperDetails.getSubName());
            // 设置再生资源企业编号
            scfSalesDetailsInfo.setRenewableSubNum(scfSubjectBasicInfoMapperDetails.getSubNum());
        } else if (roleByRowIdB.equals("2")) {
            scfSubjectBasicInfoMapperDetails = scfSubjectBasicInfoMapper.getDetails(partyBNum);
            // 从主体信息中拿到主体名称并赋值
            scfRecyclingInInfoMapperDetails.setRecyclingName(scfSubjectBasicInfoMapperDetails.getSubName());
            // 设置购方企业编号
            scfSalesDetailsInfo.setRecyclingSubNum(scfSubjectBasicInfoMapperDetails.getSubNum());
        }
        // 司机驾驶证号再去查询司机的详细信息
        String driverRowId = scfDriverInfoMapper.selectRowIdByLicense(scfRecyclingInInfoMapperDetails.getLicense());

        if (driverRowId != null) {
            ScfGetDriverDetailsVO scfDriverInfoMapperDetails = scfDriverInfoMapper.getDetails(driverRowId);
            // 取出司机姓名及司机联系方式并设置给入库单详情中
            scfRecyclingInInfoMapperDetails.setDriverName(scfDriverInfoMapperDetails.getDriverName());
            scfRecyclingInInfoMapperDetails.setDriverMobile(scfDriverInfoMapperDetails.getMobile());
        }

        // 设置利废企业入库单号
        scfSalesDetailsInfo.setWarehouseEntNum(scfRecyclingInInfoMapperDetails.getRecyclingInNum());
        // 设置购方企业名称
        scfSalesDetailsInfo.setRecyclingName(scfRecyclingInInfoMapperDetails.getRecyclingName());
        // 设置再生资源主体名称
        scfSalesDetailsInfo.setRenewableName(scfRecyclingInInfoMapperDetails.getRenewableName());
        // 设置车牌号
        scfSalesDetailsInfo.setTruckNum(scfRecyclingInInfoMapperDetails.getTruckNum());
        // 设置司机（姓名）
        scfSalesDetailsInfo.setDriverName(scfRecyclingInInfoMapperDetails.getDriverName());
        // 设置包装重
        scfSalesDetailsInfo.setPackageWeight(String.valueOf(scfRecyclingInInfoMapperDetails.getTareWeight()));
        // 设置品质扣重
        scfSalesDetailsInfo.setQualityWeight(String.valueOf(scfRecyclingInInfoMapperDetails.getDeductWeight()));
        // 设置进场实重
        scfSalesDetailsInfo.setActualWeightIn(String.valueOf(scfRecyclingInInfoMapperDetails.getEntryWeight()));
        // 设置销售总金额
        scfSalesDetailsInfo.setTotalSalesAmount(scfRecyclingInInfoMapperDetails.getSettlementAmount());
        // 设置收货日期
        scfSalesDetailsInfo.setReceiveDate(scfRecyclingInInfoMapperDetails.getRecyclingInDate());

        String WeighNumRowId = scfRecyclingWeighInfoMapper.getRowIdByWeighNum(scfRecyclingInInfoMapperDetails.getWeighNum());
        ScfGetRecyclingWeighDetailsVO recyclingWeighDetails = scfRecyclingWeighInfoMapper.getDetails(WeighNumRowId);
        // 设置重车重
        scfSalesDetailsInfo.setFullWeight(String.valueOf(recyclingWeighDetails.getFullWeight()));
        // 设置空车重
        scfSalesDetailsInfo.setEmptyWeight(String.valueOf(recyclingWeighDetails.getEmptyWeight()));
        // 利废过磅拿取再生出库单号
        // 先获取利废过磅单号-然后去利废过磅与再生出库关联关系表中查询对应的再生出库单号
        String weighNum = recyclingWeighDetails.getWeighNum();
        List<RecyclingWeighByRenewableOut> recyclingWeighByRenewableOuts = scfRecyclingWeighInfoMapper.selectRecyclingWeighByRenewableOut(weighNum);
        // 获取第一条数据
        RecyclingWeighByRenewableOut recyclingWeighByRenewableOut = recyclingWeighByRenewableOuts.get(0);
        // 在第一条数据中获取对应的再生出库单号
        String renewableOut = recyclingWeighByRenewableOut.getRenewableOutNum();
        // 通过再生出库单号查询对应的再生出库单的RowId
        String rowIdByRenewableOutNum = scfRenewableOutInfoMapper.selectRowIdByRenewableOutNum(renewableOut);
        // 获取再生出库单的详情
        ScfGetRenewableOutDetailsVO scfRenewableOutInfoMapperDetails = scfRenewableOutInfoMapper.getDetails(rowIdByRenewableOutNum);
        // 设置品名
        scfSalesDetailsInfo.setGoodsNum(scfRenewableOutInfoMapperDetails.getSpecName());
        // 设置规格
        scfSalesDetailsInfo.setStandardNum(scfRenewableOutInfoMapperDetails.getSpecName());
        // 设置销售单价
        scfSalesDetailsInfo.setUnitPrice(scfRenewableOutInfoMapperDetails.getUnitPrice());
        // 获取再生出库单号
        String renewableOutNum = scfRenewableOutInfoMapperDetails.getRenewableOutNum();
        // 通过再生出库单号查询再生出库与再生入库关联关系表
        List<ScfGetRenewableInDetailsVO> renewableOutByRenewableIns = scfRenewableOutInfoMapper.selectRenewableOutByRenewableInt(renewableOutNum);
        // 拿取第一条记录的再生入库单
        ScfGetRenewableInDetailsVO renewableOutByRenewableIn = renewableOutByRenewableIns.get(0);
        // 获取该条记录的再生入库单号
        String renewableInNum = renewableOutByRenewableIn.getRenewableInNum();
        // 通过再生入库单号码获取对应的再生入库单的rowId
        String renewableInRowId = scfRenewableInInfoMapper.selectRowIdByRenewableInNum(renewableInNum);
        // 根据再生入库单的rowId获取再生入库单的详情
        ScfGetRenewableInDetailsVO scfRenewableInInfoMapperDetails = scfRenewableInInfoMapper.getDetails(renewableInRowId);
        // 根据根据合同编号获取合同rowId
        String contractRowIdren = scfContractInfoMapper.selectRowIdByContractNum(scfRenewableInInfoMapperDetails.getContractNum(), scfRecyclingInInfoMapperDetails.getOrgCode());
        // 获取合同详细信息
        ScfGetContractDetailsVO detailsContractren = scfContractInfoMapper.getDetails(contractRowIdren);
        String partyANum1 = detailsContractren.getPartyANum();
        String roleByRowIdA1 = scfSubjectBasicInfoMapper.getRoleByRowId(partyANum1);
        // 从合同中获取再生及供应商主体编号并查询主体信息
        ScfGetSubjectDetailsVO scfSubjectBasicInfoMapperDetails2 = null;
        if (roleByRowIdA1.equals("1")) {
            scfSubjectBasicInfoMapperDetails2 = scfSubjectBasicInfoMapper.getDetails(partyANum1);
            // 从主体信息中拿到主体名称并赋值
            scfRenewableInInfoMapperDetails.setSupplierName(scfSubjectBasicInfoMapperDetails2.getSubName());
            // 设置供应商主体名称
            scfSalesDetailsInfo.setOwnerName(scfRenewableInInfoMapperDetails.getSupplierName());
            // 设置供应商编号
            scfSalesDetailsInfo.setOwnerSubNum(scfSubjectBasicInfoMapperDetails2.getSubNum());
        } else if (roleByRowIdA1.equals("3")) {
            scfSubjectBasicInfoMapperDetails2 = scfSubjectBasicInfoMapper.getDetails(partyANum1);
            // 从主体信息中拿到主体名称并赋值
            scfRenewableInInfoMapperDetails.setRenewableName(scfSubjectBasicInfoMapperDetails2.getSubName());
        }
        String partyBNum1 = detailsContractren.getPartyBNum();
        String roleByRowIdB1 = scfSubjectBasicInfoMapper.getRoleByRowId(partyBNum1);
        if (roleByRowIdB1.equals("1")) {
            scfSubjectBasicInfoMapperDetails2 = scfSubjectBasicInfoMapper.getDetails(partyBNum1);
            // 从主体信息中拿到主体名称并赋值
            scfRenewableInInfoMapperDetails.setSupplierName(scfSubjectBasicInfoMapperDetails2.getSubName());
            // 设置供应商主体名称
            scfSalesDetailsInfo.setOwnerName(scfRenewableInInfoMapperDetails.getSupplierName());
            // 设置供应商编号
            scfSalesDetailsInfo.setOwnerSubNum(scfSubjectBasicInfoMapperDetails2.getSubNum());
        } else if (roleByRowIdB1.equals("3")) {
            scfSubjectBasicInfoMapperDetails2 = scfSubjectBasicInfoMapper.getDetails(partyBNum1);
            // 从主体信息中拿到主体名称并赋值
            scfRenewableInInfoMapperDetails.setRenewableName(scfSubjectBasicInfoMapperDetails2.getSubName());
        }
        // 司机驾驶证号再去查询司机的详细信息
        String driverRowIdren = scfDriverInfoMapper.selectRowIdByLicense(scfRenewableInInfoMapperDetails.getLicense());
        ScfGetDriverDetailsVO scfDriverInfoMapperDetailsren = scfDriverInfoMapper.getDetails(driverRowIdren);
        // 取出司机姓名及司机联系方式并设置给入库单详情中
        scfRenewableInInfoMapperDetails.setDriverName(scfDriverInfoMapperDetailsren.getDriverName());
        scfRenewableInInfoMapperDetails.setDriverMobile(scfDriverInfoMapperDetailsren.getMobile());
        // 设置采购单价
        scfSalesDetailsInfo.setPurchasePrice(scfRenewableInInfoMapperDetails.getUnitPrice());
        // 设置付款重
        scfSalesDetailsInfo.setPaymentWeight(String.valueOf(recyclingWeighDetails.getGoodsWeight()));
        // 设置付给供货商货款
        scfSalesDetailsInfo.setPaymentToOwner(scfRenewableInInfoMapperDetails.getSettlementAmount());
        // 初始生成的销售明细，对账状态为0。暂未对账，其他状态为：1.对账失败 2.对账成功
        scfSalesDetailsInfo.setIsOk("0");
        // 目前 利废入库单已删除支付回单单号，用利废入库单号查询利废入库与支付关系表，获取支付回单数组
        List<RecyclingInByPayment> recyclingInByPayments = scfRecyclingInInfoMapper.selectRecyclingByPayment(recyclingInNum);
        String invoiceDate = null;
        if (recyclingInByPayments != null && !recyclingInByPayments.isEmpty()) {
            // 取集合中的第一条
            RecyclingInByPayment recyclingInByPayment = recyclingInByPayments.get(0);
            // 拿第一条数据中的支付回单号查询对应支付信息的RowId
            String selectRowIdBypaymentNum = scfPaymentInfoMapper.selectRowIdBypaymentNum(recyclingInByPayment.getPaymentNum());
            ScfGetPaymentDetailsVO scfPaymentInfoMapperDetail = scfPaymentInfoMapper.getDetail(selectRowIdBypaymentNum);
            String selectRowIdByPaymentNum = scfInvoiceInfoMapper.selectRowIdByPaymentNum(scfPaymentInfoMapperDetail.getPaymentNum());
            ScfGetInvoiceDetailsVO scfInvoiceInfoMapperDeatil = scfInvoiceInfoMapper.getDeatil(selectRowIdByPaymentNum);
            // 获取开票日期
            invoiceDate = scfInvoiceInfoMapperDeatil.getInvoiceDate();
        }
        scfSalesDetailsInfo.setInvoiceDate(invoiceDate);
        Double goodsWeight = Double.valueOf(scfSalesDetailsInfo.getPaymentWeight());
        String goodsNum = scfSalesDetailsInfo.getGoodsNum();
        Double countCarbonEmissions = null;
        if (countCarbonEmissions == null) {
            switch (goodsNum) {
                case "废铝":
                    // 处理废铝的逻辑
                    countCarbonEmissions = ScfCarbonEmissionsEnum.ALUMINUM_RECYCLING.getEmissionFactor(goodsWeight);
                    break;
                // 可以继续添加其他case语句处理不同的情况
                case "废纸":
                    // 处理废铝的逻辑
                    countCarbonEmissions = ScfCarbonEmissionsEnum.PAPER_RECYCLING.getEmissionFactor(goodsWeight);
                    break;
                // 可以继续添加其他case语句处理不同的情况
                case "废钢":
                    // 处理废铝的逻辑
                    countCarbonEmissions = ScfCarbonEmissionsEnum.STEEL_RECYCLING.getEmissionFactor(goodsWeight);
                    break;
                // 可以继续添加其他case语句处理不同的情况
                case "废塑料":
                    // 处理废铝的逻辑
                    countCarbonEmissions = ScfCarbonEmissionsEnum.PLASTIC_RECYCLING.getEmissionFactor(goodsWeight);
                    break;
                // 可以继续添加其他case语句处理不同的情况
                case "废铜":
                    // 处理废铝的逻辑
                    countCarbonEmissions = ScfCarbonEmissionsEnum.COPPER_RECYCLING.getEmissionFactor(goodsWeight);
                    break;
                case "废电器电子":
                    // 处理废铝的逻辑
                    countCarbonEmissions = ScfCarbonEmissionsEnum.ELECTRICAL_EQUIPMENT_RECYCLING.getEmissionFactor(goodsWeight);
                    break;
                // 可以继续添加其他case语句处理不同的情况
            }
            // 设置碳排放量
            scfSalesDetailsInfo.setCarbonEmissions(String.valueOf(countCarbonEmissions));
            String rowId = "XXMX" +    MD5Utils.md5( recyclingInNum  );
            scfSalesDetailsInfo.setRowId(rowId);
            // 生成销售明细数据
            save(scfSalesDetailsInfo);
        }
    }

    /**
     * 四流单据有更新时，同步删除已生成的旧销售明细记录，生成新销售明细记录
     *
     * @param recyclingInNum
     */
    @Override
    public void update(String recyclingInNum, String recyclingInRowId, String orgCode) {
        // 先删除旧的记录
        baseMapper.deleteByRecyclingInNum(recyclingInNum);
        // new一个销售明细实体类，开始组合字段
        ScfSalesDetailsInfo scfSalesDetailsInfo = new ScfSalesDetailsInfo();
        // 设置orgCode
        scfSalesDetailsInfo.setOrgCode(orgCode);

        // 获取利废入库单rowId
//        String RecyclingInRowId = scfRecyclingInInfoMapper.selectRowIdByRecyclingInNum(recyclingInNum);
        ScfGetRecyclingInDetailsVO scfRecyclingInInfoMapperDetails = scfRecyclingInInfoMapper.getDetails(recyclingInRowId);
        // 根据根据合同编号获取合同rowId
        String contractRowId = scfContractInfoMapper.selectRowIdByContractNum( scfRecyclingInInfoMapperDetails.getContractNum(), scfRecyclingInInfoMapperDetails.getOrgCode());
        // 获取合同详细信息
        ScfGetContractDetailsVO detailsContract = scfContractInfoMapper.getDetails(contractRowId);
        String partyANum = detailsContract.getPartyANum();
        String roleByRowIdA = scfSubjectBasicInfoMapper.getRoleByRowId(partyANum);
        // 从供销合同中获取再生及供应商主体编号并查询主体信息
        ScfGetSubjectDetailsVO scfSubjectBasicInfoMapperDetails = null;
        if (roleByRowIdA.equals("3")) {
            scfSubjectBasicInfoMapperDetails = scfSubjectBasicInfoMapper.getDetails(partyANum);
            // 从主体信息中拿到主体名称并赋值
            scfRecyclingInInfoMapperDetails.setRenewableName(scfSubjectBasicInfoMapperDetails.getSubName());
            // 设置再生资源企业编号
            scfSalesDetailsInfo.setRenewableSubNum(scfSubjectBasicInfoMapperDetails.getSubNum());
        } else if (roleByRowIdA.equals("2")) {
            scfSubjectBasicInfoMapperDetails = scfSubjectBasicInfoMapper.getDetails(partyANum);
            // 从主体信息中拿到主体名称并赋值
            scfRecyclingInInfoMapperDetails.setRecyclingName(scfSubjectBasicInfoMapperDetails.getSubName());
            // 设置购方企业编号
            scfSalesDetailsInfo.setRecyclingSubNum(scfSubjectBasicInfoMapperDetails.getSubNum());
        }
        String partyBNum = detailsContract.getPartyBNum();
        String roleByRowIdB = scfSubjectBasicInfoMapper.getRoleByRowId(partyBNum);
        if (roleByRowIdB.equals("3")) {
            scfSubjectBasicInfoMapperDetails = scfSubjectBasicInfoMapper.getDetails(partyBNum);
            // 从主体信息中拿到主体名称并赋值
            scfRecyclingInInfoMapperDetails.setRenewableName(scfSubjectBasicInfoMapperDetails.getSubName());
            // 设置再生资源企业编号
            scfSalesDetailsInfo.setRenewableSubNum(scfSubjectBasicInfoMapperDetails.getSubNum());
        } else if (roleByRowIdB.equals("2")) {
            scfSubjectBasicInfoMapperDetails = scfSubjectBasicInfoMapper.getDetails(partyBNum);
            // 从主体信息中拿到主体名称并赋值
            scfRecyclingInInfoMapperDetails.setRecyclingName(scfSubjectBasicInfoMapperDetails.getSubName());
            // 设置购方企业编号
            scfSalesDetailsInfo.setRecyclingSubNum(scfSubjectBasicInfoMapperDetails.getSubNum());
        }
        // 司机驾驶证号再去查询司机的详细信息
        String driverRowId = scfDriverInfoMapper.selectRowIdByLicense(scfRecyclingInInfoMapperDetails.getLicense());
        if (driverRowId != null) {
            ScfGetDriverDetailsVO scfDriverInfoMapperDetails = scfDriverInfoMapper.getDetails(driverRowId);
            // 取出司机姓名及司机联系方式并设置给入库单详情中
            scfRecyclingInInfoMapperDetails.setDriverName(scfDriverInfoMapperDetails.getDriverName());
            scfRecyclingInInfoMapperDetails.setDriverMobile(scfDriverInfoMapperDetails.getMobile());
        }


        // 设置利废企业入库单号
        scfSalesDetailsInfo.setWarehouseEntNum(scfRecyclingInInfoMapperDetails.getRecyclingInNum());
        // 设置购方企业名称
        scfSalesDetailsInfo.setRecyclingName(scfRecyclingInInfoMapperDetails.getRecyclingName());
        // 设置再生资源主体名称
        scfSalesDetailsInfo.setRenewableName(scfRecyclingInInfoMapperDetails.getRenewableName());
        // 设置车牌号
        scfSalesDetailsInfo.setTruckNum(scfRecyclingInInfoMapperDetails.getTruckNum());
        // 设置司机（姓名）
        scfSalesDetailsInfo.setDriverName(scfRecyclingInInfoMapperDetails.getDriverName());
        // 设置包装重
        scfSalesDetailsInfo.setPackageWeight(String.valueOf(scfRecyclingInInfoMapperDetails.getTareWeight()));
        // 设置品质扣重
        scfSalesDetailsInfo.setQualityWeight(String.valueOf(scfRecyclingInInfoMapperDetails.getDeductWeight()));
        // 设置进场实重
        scfSalesDetailsInfo.setActualWeightIn(String.valueOf(scfRecyclingInInfoMapperDetails.getEntryWeight()));
        // 设置销售总金额
        scfSalesDetailsInfo.setTotalSalesAmount(scfRecyclingInInfoMapperDetails.getSettlementAmount());
        // 设置收货日期
        scfSalesDetailsInfo.setReceiveDate(scfRecyclingInInfoMapperDetails.getRecyclingInDate());

        String WeighNumRowId = scfRecyclingWeighInfoMapper.getRowIdByWeighNum(scfRecyclingInInfoMapperDetails.getWeighNum());
        ScfGetRecyclingWeighDetailsVO recyclingWeighDetails = scfRecyclingWeighInfoMapper.getDetails(WeighNumRowId);
        // 设置重车重
        scfSalesDetailsInfo.setFullWeight(String.valueOf(recyclingWeighDetails.getFullWeight()));
        // 设置空车重
        scfSalesDetailsInfo.setEmptyWeight(String.valueOf(recyclingWeighDetails.getEmptyWeight()));
        // 利废过磅拿取再生出库单号
        // 先获取利废过磅单号-然后去利废过磅与再生出库关联关系表中查询对应的再生出库单号
        String weighNum = recyclingWeighDetails.getWeighNum();
        List<RecyclingWeighByRenewableOut> recyclingWeighByRenewableOuts = scfRecyclingWeighInfoMapper.selectRecyclingWeighByRenewableOut(weighNum);
        if (!recyclingWeighByRenewableOuts.isEmpty()) {
            // 获取第一条数据
            RecyclingWeighByRenewableOut recyclingWeighByRenewableOut = recyclingWeighByRenewableOuts.get(0);
            // 在第一条数据中获取对应的再生出库单号
            String renewableOut = recyclingWeighByRenewableOut.getRenewableOutNum();
            // 通过再生出库单号查询对应的再生出库单的RowId
            String rowIdByRenewableOutNum = scfRenewableOutInfoMapper.selectRowIdByRenewableOutNum(renewableOut);
            // 获取再生出库单的详情
            ScfGetRenewableOutDetailsVO scfRenewableOutInfoMapperDetails = scfRenewableOutInfoMapper.getDetails(rowIdByRenewableOutNum);
            // 设置品名
            scfSalesDetailsInfo.setGoodsNum(scfRenewableOutInfoMapperDetails.getSpecName());
            // 设置规格
            scfSalesDetailsInfo.setStandardNum(scfRenewableOutInfoMapperDetails.getSpecName());
            // 设置销售单价
            scfSalesDetailsInfo.setUnitPrice(scfRenewableOutInfoMapperDetails.getUnitPrice());
            // 获取再生出库单号
            String renewableOutNum = scfRenewableOutInfoMapperDetails.getRenewableOutNum();
            // 通过再生出库单号查询再生出库与再生入库关联关系表
            List<ScfGetRenewableInDetailsVO> renewableOutByRenewableIns = scfRenewableOutInfoMapper.selectRenewableOutByRenewableInt(renewableOutNum);
            // 拿取第一条记录的再生入库单
            ScfGetRenewableInDetailsVO renewableOutByRenewableIn = renewableOutByRenewableIns.get(0);
            // 获取该条记录的再生入库单号
            String renewableInNum = renewableOutByRenewableIn.getRenewableInNum();
            // 通过再生入库单号码获取对应的再生入库单的rowId
            String renewableInRowId = scfRenewableInInfoMapper.selectRowIdByRenewableInNum(renewableInNum);
            // 根据再生入库单的rowId获取再生入库单的详情
            ScfGetRenewableInDetailsVO scfRenewableInInfoMapperDetails = scfRenewableInInfoMapper.getDetails(renewableInRowId);
            // 根据根据合同编号获取合同rowId
            String contractRowIdren = scfContractInfoMapper.selectRowIdByContractNum(  scfRenewableInInfoMapperDetails.getContractNum(), scfRenewableInInfoMapperDetails.getOrgCode());
            // 获取合同详细信息
            ScfGetContractDetailsVO detailsContractren = scfContractInfoMapper.getDetails(contractRowIdren);
            String partyANum1 = detailsContractren.getPartyANum();
            String roleByRowIdA1 = scfSubjectBasicInfoMapper.getRoleByRowId(partyANum1);
            // 从合同中获取再生及供应商主体编号并查询主体信息
            ScfGetSubjectDetailsVO scfSubjectBasicInfoMapperDetails2 = null;
            if (roleByRowIdA1.equals("1")) {
                scfSubjectBasicInfoMapperDetails2 = scfSubjectBasicInfoMapper.getDetails(partyANum1);
                // 从主体信息中拿到主体名称并赋值
                scfRenewableInInfoMapperDetails.setSupplierName(scfSubjectBasicInfoMapperDetails2.getSubName());
                // 设置供应商主体名称
                scfSalesDetailsInfo.setOwnerName(scfRenewableInInfoMapperDetails.getSupplierName());
                // 设置供应商编号
                scfSalesDetailsInfo.setOwnerSubNum(scfSubjectBasicInfoMapperDetails2.getSubNum());
            } else if (roleByRowIdA1.equals("3")) {
                scfSubjectBasicInfoMapperDetails2 = scfSubjectBasicInfoMapper.getDetails(partyANum1);
                // 从主体信息中拿到主体名称并赋值
                scfRenewableInInfoMapperDetails.setRenewableName(scfSubjectBasicInfoMapperDetails2.getSubName());
            }
            String partyBNum1 = detailsContractren.getPartyBNum();
            String roleByRowIdB1 = scfSubjectBasicInfoMapper.getRoleByRowId(partyBNum1);
            if (roleByRowIdB1.equals("1")) {
                scfSubjectBasicInfoMapperDetails2 = scfSubjectBasicInfoMapper.getDetails(partyBNum1);
                // 从主体信息中拿到主体名称并赋值
                scfRenewableInInfoMapperDetails.setSupplierName(scfSubjectBasicInfoMapperDetails2.getSubName());
                // 设置供应商主体名称
                scfSalesDetailsInfo.setOwnerName(scfRenewableInInfoMapperDetails.getSupplierName());
                // 设置供应商编号
                scfSalesDetailsInfo.setOwnerSubNum(scfSubjectBasicInfoMapperDetails2.getSubNum());
            } else if (roleByRowIdB1.equals("3")) {
                scfSubjectBasicInfoMapperDetails2 = scfSubjectBasicInfoMapper.getDetails(partyBNum1);
                // 从主体信息中拿到主体名称并赋值
                scfRenewableInInfoMapperDetails.setRenewableName(scfSubjectBasicInfoMapperDetails2.getSubName());
            }
            // 司机驾驶证号再去查询司机的详细信息
            String driverRowIdren = scfDriverInfoMapper.selectRowIdByLicense(scfRenewableInInfoMapperDetails.getLicense());
            ScfGetDriverDetailsVO scfDriverInfoMapperDetailsren = scfDriverInfoMapper.getDetails(driverRowIdren);
            // 取出司机姓名及司机联系方式并设置给入库单详情中
            scfRenewableInInfoMapperDetails.setDriverName(scfDriverInfoMapperDetailsren.getDriverName());
            scfRenewableInInfoMapperDetails.setDriverMobile(scfDriverInfoMapperDetailsren.getMobile());
            // 设置采购单价
            scfSalesDetailsInfo.setPurchasePrice(scfRenewableInInfoMapperDetails.getUnitPrice());
            // 设置付款重
            scfSalesDetailsInfo.setPaymentWeight(String.valueOf(recyclingWeighDetails.getGoodsWeight()));
            // 设置付给供货商货款
            scfSalesDetailsInfo.setPaymentToOwner(scfRenewableInInfoMapperDetails.getSettlementAmount());

        }


        // 初始生成的销售明细，对账状态为0。暂未对账，其他状态为：1.对账失败 2.对账成功
        scfSalesDetailsInfo.setIsOk("0");
        // 目前 利废入库单已删除支付回单单号，用利废入库单号查询利废入库与支付关系表，获取支付回单数组
        List<RecyclingInByPayment> recyclingInByPayments = scfRecyclingInInfoMapper.selectRecyclingByPayment(recyclingInNum);
        String invoiceDate = null;
        if (recyclingInByPayments != null && !recyclingInByPayments.isEmpty()) {
            // 取集合中的第一条
            RecyclingInByPayment recyclingInByPayment = recyclingInByPayments.get(0);
            // 拿第一条数据中的支付回单号查询对应支付信息的RowId
            String selectRowIdBypaymentNum = scfPaymentInfoMapper.selectRowIdBypaymentNum(recyclingInByPayment.getPaymentNum());
            ScfGetPaymentDetailsVO scfPaymentInfoMapperDetail = scfPaymentInfoMapper.getDetail(selectRowIdBypaymentNum);
            String selectRowIdByPaymentNum = scfInvoiceInfoMapper.selectRowIdByPaymentNum(scfPaymentInfoMapperDetail.getPaymentNum());

            ScfGetInvoiceDetailsVO scfInvoiceInfoMapperDeatil = scfInvoiceInfoMapper.getDeatil(selectRowIdByPaymentNum);
            if (scfInvoiceInfoMapperDeatil != null) {
                // 获取开票日期
                invoiceDate = scfInvoiceInfoMapperDeatil.getInvoiceDate();
            }

        }
        scfSalesDetailsInfo.setInvoiceDate(invoiceDate);


        Double goodsWeight = Double.valueOf(scfSalesDetailsInfo.getPaymentWeight() != null ? scfSalesDetailsInfo.getPaymentWeight() : "0");
        String goodsNum = scfSalesDetailsInfo.getGoodsNum();
        Double countCarbonEmissions = null;
        if (countCarbonEmissions == null && goodsNum != null) {
            switch (goodsNum) {
                case "废铝":
                    // 处理废铝的逻辑
                    countCarbonEmissions = ScfCarbonEmissionsEnum.ALUMINUM_RECYCLING.getEmissionFactor(goodsWeight);
                    break;
                // 可以继续添加其他case语句处理不同的情况
                case "废纸":
                    // 处理废铝的逻辑
                    countCarbonEmissions = ScfCarbonEmissionsEnum.PAPER_RECYCLING.getEmissionFactor(goodsWeight);
                    break;
                // 可以继续添加其他case语句处理不同的情况
                case "废钢":
                    // 处理废铝的逻辑
                    countCarbonEmissions = ScfCarbonEmissionsEnum.STEEL_RECYCLING.getEmissionFactor(goodsWeight);
                    break;
                // 可以继续添加其他case语句处理不同的情况
                case "废塑料":
                    // 处理废铝的逻辑
                    countCarbonEmissions = ScfCarbonEmissionsEnum.PLASTIC_RECYCLING.getEmissionFactor(goodsWeight);
                    break;
                // 可以继续添加其他case语句处理不同的情况
                case "废铜":
                    // 处理废铝的逻辑
                    countCarbonEmissions = ScfCarbonEmissionsEnum.COPPER_RECYCLING.getEmissionFactor(goodsWeight);
                    break;
                case "废电器电子":
                    // 处理废铝的逻辑
                    countCarbonEmissions = ScfCarbonEmissionsEnum.ELECTRICAL_EQUIPMENT_RECYCLING.getEmissionFactor(goodsWeight);
                    break;
                // 可以继续添加其他case语句处理不同的情况
            }
            // 设置碳排放量
            scfSalesDetailsInfo.setCarbonEmissions(String.valueOf(countCarbonEmissions));
        }
        String rowId = "XXMX" +    MD5Utils.md5( recyclingInNum  );
        scfSalesDetailsInfo.setRowId(rowId);
        // 生成销售明细数据
        save(scfSalesDetailsInfo);
    }

    /**
     * 销售明细-导入文件进行与生成的明细比对
     *
     * @param operatorId
     * @param file
     * @return
     * @throws IOException
     */
    @Override
    public void isSalesDetails(String operatorId, MultipartFile file) throws IOException {
        boolean isOK = false;
        // 获取文件的Base64编码
        byte[] fileBytes = file.getBytes();
        // 解析excel文件获取数据集合
        List<ScfSalesDetailsInfo> scfSalesDetailsInfosExcel = ExcelUtil.readExcel(fileBytes, ScfSalesDetailsInfo.class, "Sheet1", 3);
        List<ScfSalesDetailsInfo> scfSalesDetailsInfos = baseMapper.selectAll();

        for (ScfSalesDetailsInfo excelInfo : scfSalesDetailsInfosExcel) {
            String excelWarehouseEntNum = excelInfo.getWarehouseEntNum();

            // 是否找到对应数据的标志
            boolean found = false;

            for (ScfSalesDetailsInfo dbInfo : scfSalesDetailsInfos) {
                // 在 scfSalesDetailsInfos 中查找相同 warehouseEntNum 的数据
                if (Objects.equals(dbInfo.getWarehouseEntNum(), excelWarehouseEntNum)) {
                    found = true;
                    DateConversionExample dateConversionExample = new DateConversionExample();
                    StringUtil stringUtil = new StringUtil();
                    // 判断其他字段是否一致
                    if (!Objects.equals(excelInfo.getOwnerName(), dbInfo.getOwnerName())
                            || !Objects.equals(dateConversionExample.formatDateString(excelInfo.getReceiveDate()), dbInfo.getReceiveDate())
                            || !Objects.equals(excelInfo.getGoodsNum(), dbInfo.getGoodsNum())
                            || !Objects.equals(stringUtil.removeTrailingZeros(excelInfo.getPaymentWeight()), stringUtil.removeTrailingZeros(dbInfo.getPaymentWeight()))
                            || !Objects.equals(stringUtil.removeTrailingZeros(String.valueOf(excelInfo.getUnitPrice())), stringUtil.removeTrailingZeros(String.valueOf(dbInfo.getUnitPrice())))
                            || !Objects.equals(stringUtil.removeTrailingZeros(String.valueOf(excelInfo.getTotalSalesAmount())), stringUtil.removeTrailingZeros(String.valueOf(dbInfo.getTotalSalesAmount())))
                            || !Objects.equals(excelInfo.getDriverName(), dbInfo.getDriverName())
                            || !Objects.equals(stringUtil.removeTrailingZeros(String.valueOf(excelInfo.getPaymentToOwner())), stringUtil.removeTrailingZeros(String.valueOf(dbInfo.getPaymentToOwner())))
                            || !Objects.equals(dateConversionExample.formatDateString(excelInfo.getInvoiceDate()), dbInfo.getInvoiceDate())) {
                        // 不一致则设置 isOk 字段的值为1
                        dbInfo.setIsOk("1");
                    } else {
                        // 一致则设置 isOk 字段的值为2
                        dbInfo.setIsOk("2");
                    }

                    // 退出内层循环，因为已经找到对应数据
                    break;
                }
            }
        }

        // 更新数据库中的数据
        if (!scfSalesDetailsInfos.isEmpty()) {
            baseMapper.updateBatch(scfSalesDetailsInfos);
        }
    }
}
